package controller

import (
	"encoding/json"
	"fmt"
	"service/components"
	"service/modules"
	"service/utils"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

// AddAttributeController 添加商品的展示属性
func AddAttributeController(ctx *gin.Context) {
	var cc modules.Attribute
	ctx.ShouldBind(&cc)
	count := modules.GetAttributeByNameAndTypeService(cc)
	if count != 0 {
		utils.HandleFailResponse(ctx, cc.Name+" 标签已存在", nil)
		return
	}
	fmt.Println("cc: ", cc, count)
	cc.Uid = 1
	cc.CreatedAt = time.Now()
	err := modules.AddAttributeService(&cc)
	if err != nil {
		utils.HandleFailResponse(ctx, "增加商品售卖属性失败", nil)
		return
	}
	utils.HandleSuccessResponse(ctx, "添加成功!!", nil)
}

// GetAllAttributeController 获取商品的所有展示属性
func GetAllAttributeController(ctx *gin.Context) {
	_type := ctx.Query("type")
	display := ctx.DefaultQuery("display", "1")

	cc, err := modules.GetAllAttributeService(_type, display)
	var parent []modules.Attribute
	var children []modules.Attribute
	if err != nil {
		utils.HandleFailResponse(ctx, "查询失败", nil)
		return
	}
	for _, attribute := range cc {
		if attribute.IsParent == 0 {
			parent = append(parent, attribute)
		} else {
			children = append(children, attribute)
		}
	}
	finally := make([]modules.Attribute, len(parent))
	for i := 0; i < len(parent); i++ {
		finally[i] = parent[i]
		for j := 0; j < len(children); j++ {
			if parent[i].Id == children[j].IsParent {
				finally[i].Children = append(finally[i].Children, &children[j])
			}
		}
	}
	for i := 0; i < len(finally); i++ {
		for j := 0; j < len(finally[i].Children); j++ {
			for k := 0; k < len(children); k++ {
				if finally[i].Children[j].Id == children[k].IsParent {
					finally[i].Children[j].Children = append(finally[i].Children[j].Children, &children[k])
				}
			}
		}
	}
	utils.HandleSuccessResponse(ctx, "查询商品属性成功", finally)
}

func PatchAttributeController() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		raw, _ := ctx.GetRawData()
		var data map[string]any
		err := json.Unmarshal(raw, &data)
		if err != nil {
			utils.HandleFailResponse(ctx, "数据绑定失败", nil)
			return
		}
		if data["value"] == nil {
			fmt.Println("更新显示和隐藏", data["display"])
			err = components.DB.Model(modules.Attribute{}).
				Where("id = ?", data["id"]).
				Update("display", data["display"]).Error
		} else {
			err = components.DB.Model(modules.Attribute{}).
				Where("id = ?", data["id"]).
				Update("name", data["value"]).Error
			id, _ := strconv.Atoi(fmt.Sprintf("%v", data["id"]))
			if id >= 99 && id <= 105 {
				err = components.DB.Model(&modules.File{}).
					Where("attribute = ?", data["name"]).
					Updates(map[string]interface{}{
						"attribute": data["value"],
					}).Error
				if err != nil {
					utils.HandleFailResponse(ctx, "更新文件类失败", nil)
					return
				}
			}
		}
		if err != nil {
			utils.HandleFailResponse(ctx, "更新失败", err.Error())
			return
		}
		utils.HandleSuccessResponse(ctx, "更新成功", nil)
	}
}

// DeleteAttributeController 删除商品的展示属性
func DeleteAttributeController(ctx *gin.Context) {
	c := new(modules.Attribute)
	ctx.ShouldBind(c)
	fmt.Println("c: ", c)
	err := modules.DeleteAttributeService(c)
	if err != nil {
		utils.HandleFailResponse(ctx, "属性删除失败!", nil)
		return
	}
	utils.HandleSuccessResponse(ctx, "属性删除成功~", nil)
}
